#ifndef VZCONN_SERVER_KVDB_SQLITE_H_
#define VZCONN_SERVER_KVDB_SQLITE_H_

#include "glog/logging.h"
#include "sqlite3/sqlite3.h"
#include "vzconn/base/basicdefines.h"

namespace kvdb {

class KvdbSqlite : public boost::noncopyable,
  public boost::enable_shared_from_this < KvdbSqlite >  {
 public:
  typedef boost::shared_ptr<KvdbSqlite> Ptr;
  KvdbSqlite();
  virtual ~KvdbSqlite();
  bool InitKvdb(const char *kvdb_path);
  void UinitKvdb();
  bool ReplaceKeyValue(const char *key, int key_size,
                       const char *value, int value_size);
  bool DeleteKeyValue(const char *key, int key_size);
  bool SelectKeyValue(const char *key, int key_size,
                      std::vector<char> &buffer);

  void ForceTransaction();
 private:
  void CheckTransaction();
  bool InitStmt();
  bool CheckFileExsits(const char *kvdb_path);
 private:
  static const unsigned int MAX_TRANSACTION_SIZE = 64;
  static const unsigned int SIZE_OF_HEADER       = 5;
  sqlite3 *db_instance_;
  sqlite3_stmt *replace_stmt_;
  sqlite3_stmt *delete_stmt_;
  sqlite3_stmt *select_stmt_;
  unsigned int is_transaction_;
};

}

#endif //  VZCONN_SERVER_KVDB_SQLITE_H_
